package com.heima.乐观锁;

//juc: java并发编码
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * @author 陈辉
 * @data 2023 10:10
 */

public class Demo {
    //AtomicInteger: 用来替代int, Integer
    public static AtomicInteger money = new AtomicInteger(0);


    public static void main(String[] args) throws InterruptedException {

        //线程计数器
        CountDownLatch latch = new CountDownLatch(2);

        //t1线程
        new Thread(() -> {
            for (int i = 1; i <= 100000; i++) {
                money.getAndIncrement();
            }

            //计数器门栓 -1
            System.out.println("t1完事了...");
            latch.countDown();
        }).start();

        //t2线程
        new Thread(() -> {
            for (int i = 1; i <= 100000; i++) {
                money.getAndIncrement();
            }

            //计数器门栓 -1
            System.out.println("t2完事了...");
            latch.countDown();
        }).start();


        //基于门栓阻塞：当所有门栓都消除，这里的阻塞就会解除
        System.out.println("我在等着你们解除门栓呢.....");
        latch.await();
        System.out.println("你们终于结束了.....");
        //期望：200
        System.out.println("money = " + money);
    }
}
